package client

import (
	"context"
	"google.golang.org/grpc"
	"travel/rpc/proto/travel"
)

type Handler func(ctx context.Context, client travel.TravelClient) (interface{}, error)

func UserClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8085", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := travel.NewTravelClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	defer dial.Close()
	return res, nil
}

func Register(ctx context.Context, i *travel.RegisterReq) (*travel.RegisterResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		register, err := client.Register(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.RegisterResp), nil
}

func Login(ctx context.Context, i *travel.LoginReq) (*travel.LoginResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		login, err := client.Login(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.LoginResp), nil
}

func GoodsCreate(ctx context.Context, i *travel.GoodsCreateReq) (*travel.GoodsCreateResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		create, err := client.GoodsCreate(ctx, i)
		if err != nil {
			return nil, err
		}
		return create, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.GoodsCreateResp), nil
}

func RealName(ctx context.Context, i *travel.RealNameReq) (*travel.RealNameResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		name, err := client.RealName(ctx, i)
		if err != nil {
			return nil, err
		}
		return name, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.RealNameResp), nil
}

func OrderSn(ctx context.Context, i *travel.OrderSnReq) (*travel.OrderSnResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		sn, err := client.OrderSn(ctx, i)
		if err != nil {
			return nil, err
		}
		return sn, nil
	})
	if err != nil {
		return nil, err

	}
	return client.(*travel.OrderSnResp), nil
}

func Insure(ctx context.Context, i *travel.InsureReq) (*travel.InsureResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		insure, err := client.Insure(ctx, i)
		if err != nil {
			return nil, err
		}
		return insure, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.InsureResp), nil
}

func Callback(ctx context.Context, i *travel.CallbackReq) (*travel.CallbackResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		callback, err := client.Callback(ctx, i)
		if err != nil {
			return nil, err
		}
		return callback, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.CallbackResp), nil
}

func ScenicSo(ctx context.Context, i *travel.ScenicSoReq) (*travel.ScenicSoResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		so, err := client.ScenicSo(ctx, i)
		if err != nil {
			return nil, err
		}
		return so, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.ScenicSoResp), nil
}

func OrderFind(ctx context.Context, i *travel.OrderFindReq) (*travel.OrderFindResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		find, err := client.OrderFind(ctx, i)
		if err != nil {
			return nil, err
		}
		return find, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.OrderFindResp), nil
}

func Refund(ctx context.Context, i *travel.RefundReq) (*travel.RefundResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client travel.TravelClient) (interface{}, error) {
		refund, err := client.Refund(ctx, i)
		if err != nil {
			return nil, err
		}
		return refund, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.RefundResp), nil
}
